﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DogJson
{

    public static class CollectionManager
    {
        public static void Start()
        {
            var assembly = Assembly.GetAssembly(typeof(ICollectionArrayBase));

            ChakeAssembly(assembly);
            assembly = Assembly.GetExecutingAssembly();
            ChakeAssembly(assembly);
            assembly = Assembly.GetCallingAssembly();
            ChakeAssembly(assembly);
            return;
        }

        private static void ChakeAssembly(Assembly assembly)
        {
            foreach (var t in assembly.GetTypes())
            {
                CollectionAttribute attribute = t.GetCustomAttribute<CollectionAttribute>();
                if (attribute != null)
                {
                    if (attribute.isArrays)
                    {
                        formatterArrayTypeMap[attribute.type] = t;
                        if (!attribute.type.IsGenericType)
                        {
                            formatterAllArrayMap[attribute.type] = (ICollectionArrayBase)System.Activator.CreateInstance(t);
                        }
                    }
                    else
                    {
                        formatterObjectTypeMap[attribute.type] = t;
                        if (!attribute.type.IsGenericType)
                        {
                            formatterAllObjectMap[attribute.type] = (ICollectionObjectBase)System.Activator.CreateInstance(t);
                        }
                    }
                }
            }
        }



        public static readonly Dictionary<Type, Type> formatterArrayTypeMap = new Dictionary<Type, Type>();

        public static readonly Dictionary<Type, Type> formatterObjectTypeMap = new Dictionary<Type, Type>();

        public static readonly Dictionary<Type, ICollectionArrayBase> formatterAllArrayMap = new Dictionary<Type, ICollectionArrayBase>();

        public static readonly Dictionary<Type, ICollectionObjectBase> formatterAllObjectMap = new Dictionary<Type, ICollectionObjectBase>();



    }

}
